<template>
  <div>
    <h2 align="center">添加理财信息视图</h2>
    <table class="table table-bordered">
      <tr>
        <td>理财产品名称:</td>
        <td>
          <input type="text" v-model="addFinanceData.F_Name">
        </td>
      </tr>

      <tr>
        <td>年收益</td>
        <td>
          <input type="text" v-model="addFinanceData.F_Year" >
        </td>
      </tr>

      <tr>
        <td>最低金额</td>
        <td>
          <input type="text" v-model="addFinanceData.F_MinMoney" >
        </td>
      </tr>

      <tr>
        <td>风险等级</td>
        <td>
          <input type="radio" name="level" :value="0" v-model="addFinanceData.F_Level" >低风险
          <input type="radio" name="level" :value="1" v-model="addFinanceData.F_Level" >中风险
          <input type="radio" name="level" :value="2" v-model="addFinanceData.F_Level" >高风险
        </td>
      </tr>

      <tr>
        <td>利息</td>
        <td>
          <select v-model="addFinanceData.FT_Id">
            <option value="0">==请选择==</option>
            <option v-for="item in list" :value="item.FT_Id">
              {{item.FT_Year}}年{{ (item.FT_Numver)*100 }}%
            </option>
          </select>
        </td>
      </tr>

      <tr>
        <td></td>
        <td>
          <input type="button" value="保存" @click="AddFinance" >
        </td>
      </tr>
    </table>
  </div>
</template>

<script setup lang="ts">

import {onMounted, reactive,toRefs} from 'vue';

import { useRouter } from 'vue-router';

import axios from 'axios';

//路由
let router = useRouter();

//数据
let data:any =reactive({
  list:[],
  //添加理财信息
   addFinanceData:{
    F_Name:'',
    F_Year:'',
    F_MinMoney:'',
    T_Level:0,
    FT_Id:0
   }
})

//解构
let {list,addFinanceData} = toRefs(data);

onMounted(()=>{
  GetFinanceInterest()
})

//理财  利息表
const GetFinanceInterest=()=>{
  axios({
    url:'/api/Finance/GetFinanceInterestList',
    method:'get'
  }).then((res)=>{
    console.log(res);
    list.value=res.data.data
  }).catch(err=>{
    console.log(err);
  })
}

//添加理财信息
const AddFinance =()=>{
  //非空判断
  if(addFinanceData.value.F_Name==""){
    return alert('理财名称不能为空')
  }
  if(addFinanceData.value.F_Year==""){
    return alert('年收益不能为空')
  }
  //数字判断
  if(isNaN(addFinanceData.value.F_Year)){
    return alert('年收益必须为数字')
  }
  if(addFinanceData.value.F_Year<0){
    return alert('年收益必须大于0')
  }
  if(addFinanceData.value.F_MinMoney==""){
    return alert('最低金额不能为空')
  }
  //数字判断
  if(isNaN(addFinanceData.value.F_MinMoney)){
    return alert('最低金额必须为数字')
  }
  if(addFinanceData.value.F_MinMoney<0){
    return alert('最低金额必须大于0')
  }
  //风险等级判断
  if(addFinanceData.value.FT_Id==0){
    return alert('利息必须选择')
  }
  //发送请求
  axios({
    url:'/api/Finance/PostAddFinance',
    method:'post',
    data:addFinanceData.value
  }).then((res)=>{
    console.log(res);
    if(res.data.data==-1){
      return alert('该理财名称已存在')
    }else if(res.data.data>0){
      alert('添加成功')
      router.push('/main/adminFinanceList')
    }else{
      alert('添加失败')
    }
  }).catch(err=>{
    console.log(err);
  })
}

</script>

<style scoped>

</style>
